// 封装dao层
const { pool } = require('./pool');

// 查询所有的单词
let findWords = (callback)=>{
    pool.getConnection((err,connection)=>{
        if(err) {
            console.log(err);
        } else {
            let sql = 'select * from words'
            connection.query(sql,(err,result)=>{
                if(err) {
                    console.log(err)
                } else {
                    callback(result)
                    connection.release()
                    connection.destroy()
                }
            })
        }
    })
}


// 查询单词数量
let findAllnum = (callback)=>{
    pool.getConnection((err,connection)=>{
        if(err) {
            console.log(err);
        } else {
            // let sql = 'select count as totalcount from words'
            let sql = 'select count(id) from words'
            connection.query(sql,(err,result)=>{
                if(err) {
                    console.log(err)
                } else {
                    callback(result)
                    connection.release()
                    connection.destroy()
                }
            })
        }
    })
}

// 根据id查询单词信息
let findById = (id,callback)=>{
    pool.getConnection((err,connection)=>{
        if(err) {
            console.log(err);
        } else {
            let sql = 'select * from words where id=?'
            connection.query(sql,[id],(err,result)=>{
                if(err) {
                    console.log(err)
                } else {
                    callback(result)
                    connection.release()
                    connection.destroy()
                }
            })
        }
    })
}

// 根据word模糊查询单词信息
let findByWord = (word,callback)=>{
    pool.getConnection((err,connection)=>{
        if(err) {
            console.log(err);
        } else {
            let sql = "select * from words where word like ?"
            connection.query(sql,[word],(err,result)=>{
                if(err) {
                    console.log(err)
                } else {
                    callback(result)
                    connection.release()
                    connection.destroy()
                }
            })
        }
    })
}

// 新增或修改单词
let saveWord = (data,callback)=>{
    pool.getConnection((err,connection)=>{
        if(err) {
            console.log(err);
        } else {
            if(data.id) {
                // 修改
                let sql = 'update words set word=?,mean1=?,mean2=? where id=?'
                connection.query(sql,[data.word,data.mean1,data.mean2,data.id],(err,result)=>{
                    if(err) {
                        console.log(err)
                    } else {
                        result.code = 200
                        result.message = '编辑成功'
                        callback(result)
                        connection.release()
                        connection.destroy()
                    }
                })
            } else {
                // 保存
                let sql = 'insert into words(id,word,mean1,mean2) values(?,?,?,?)'
                connection.query(sql,[null,data.word,data.mean1,data.mean2],(err,result)=>{
                    if(err) {
                        console.log(err)
                    } else {
                        result.code = 200
                        result.message = '添加成功'
                        callback(result)
                        connection.release()
                        connection.destroy()
                    }
                })
            }
        }
    })
}

// 根据id删除单词
let deleteById = (id,callback)=>{
    pool.getConnection((err,connection)=>{
        if(err) {
            console.log(err);
        } else {
            let sql = 'delete from words where id = ?'
            connection.query(sql,[id],(err,result)=>{
                if(err) {
                    console.log(err)
                } else {
                    result.code = 200
                    result.message = '删除成功'
                    callback(result)
                    connection.release()
                    connection.destroy()
                }
            })
        }
    })
}

module.exports = {
    findWords,
    findById,
    saveWord,
    deleteById,
    findByWord,
    findAllnum
}